// pages/teacher_m_report_box/teacher_m_report_box.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    boxId:"",
    box:{},
    scoreItems:[],
    scoreItemVisible:false,
    selectedScoreItem:"",
    showScoreItemInput:false,
    scoreItem:"",
    isCloseCheck:false,
    score_item_options:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options != null){
      const boxId= options.boxId;
      this.setData({boxId})
      var that = this;
      wx.request({
        url: app.globalData.url+'/wechat/box?boxId='+boxId,
        method: 'get',
        success: res2 => {
          const scoreItems=res2.data&&res2.data.scoreItems||[];
          const score_item_options=res2.data&&res2.data.score_item_options||{};
          const selectedScoreItem=scoreItems[0];
          const score_item_option=score_item_options[selectedScoreItem]||{}
          this.setData({
            box:res2.data||{},
            scoreItems,
            selectedScoreItem,
            score_item_options:score_item_options,
            isCloseCheck:score_item_option.isCloseCheck
          });
            wx.setNavigationBarTitle({
            title: res2.data&&res2.data.name||"未查到课程"
        })
        }
      })
    }
  },
  onClickPicker:function(){
    console.log('onClickPicker',this.data.scoreItems)
    this.setData({scoreItemVisible:true})
  },
  onPickerCancel:function(){
    this.setData({scoreItemVisible:false})
  },
  onPickScoreItem:function(e){
    const selectedScoreItem=e.target.id;
    app.globalData.score_item=selectedScoreItem;
    const score_item_options=this.data.score_item_options||{};
    const score_item_option=score_item_options[selectedScoreItem]||{};
    console.log("onPickScoreItem",score_item_option,score_item_options)
    this.setData({selectedScoreItem, isCloseCheck:score_item_option.isCloseCheck||false})
  },

  click_prepare:function(){
    wx.navigateTo({
      url: "../teacher_prepare/prepare?boxId="+this.data.boxId+"&score_item="+this.data.selectedScoreItem,
    })
  },

  current_reports:function(){
    wx.navigateTo({
      url: "../teacher_current_reports/teacher_current_reports?boxId="+this.data.boxId+"&score_item="+this.data.selectedScoreItem,
    })
  },

  after_reports:function(){
    wx.navigateTo({
      url: "../teacher_report_box/report_box?boxId="+this.data.boxId+"&score_item="+this.data.selectedScoreItem,
    })
  },
  change_check_status:function(){
      const isCloseCheck=this.data.isCloseCheck;
       const {score_item_options,selectedScoreItem}=this.data;
      const score_item_option=score_item_options[selectedScoreItem]||{};
      score_item_option.isCloseCheck=!isCloseCheck;
      score_item_options[selectedScoreItem]=score_item_option
      wx.request({
        url: app.globalData.url+'/wechat/update_report_box',
        data: {
          id: this.data.boxId,
          key:"score_item_options",
          value:score_item_options,
          token:app.globalData.token
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          this.setData({isCloseCheck:!isCloseCheck})
        }
      })
      
  },

  set_report_box:function(){
    wx.navigateTo({
      url: "../report_box_setting/report_box_setting?boxId="+this.data.boxId,
    })
  },
  showDialog: function(){
    this.setData({showScoreItemInput:true})
  },
  closeDialog: function(){
    this.setData({showScoreItemInput:false})
  },
  creatScoreItem: function(){
    var that = this;
  
    if (this.data.scoreItem == "") {
      wx.showToast({
        title: '请输入作业项目名称',
        icon: 'none',
        duration: 2000
      })
    }else{
      const scoreItems=this.data.scoreItems;
      scoreItems.push(this.data.scoreItem);
      wx.request({
        url: app.globalData.url+'/wechat/update_score_item',
        data: {
          id: that.data.boxId,
          value: scoreItems,
          token:app.globalData.token,
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
            this.setData({showScoreItemInput:false,
              box:res2.data,
              scoreItems})
        }
      })
    }
  },
  changeScoreItem:function(e){
    this.setData({
      scoreItem: e.detail.value
    })
  },

  showDeleteConfirm: function () {
    wx.showModal({
      title: '提示',
      content: `确定要删除${this.data.selectedScoreItem}吗？`,
      success: (res) => {
        if (res.confirm) {
          this.removeScoreItem()
        }
      }
    })
  },

  removeScoreItem:function(){
    const scoreItems=this.data.scoreItems;
    const scoreItem=this.data.selectedScoreItem;
    const _scoreItems=scoreItems.filter(s=>s!==scoreItem);
    const that=this;
    wx.request({
      url: app.globalData.url+'/wechat/update_score_item',
      data: {
        id: that.data.boxId,
        value: _scoreItems,
        token:app.globalData.token,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: res2 => {
        this.setData({scoreItems:_scoreItems,selectedScoreItem:_scoreItems[0]});
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})